package com.newbingo.dao;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Criteria;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.criterion.Expression;
import org.hibernate.criterion.Projection;
import org.hibernate.criterion.Projections;
import org.hibernate.transform.ResultTransformer;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.newbingo.dao.imple.IGamePlayDao;
import com.newbingo.entity.Game;
import com.newbingo.entity.GamePlay;
import com.newbingo.util.Pager;

public class GamePlayDao extends HibernateDaoSupport implements IGamePlayDao {
	private static final Log log = LogFactory.getLog(GamePlayDao.class);
	
	/**
	 *	保存游戏玩法
	 */
	public void save(GamePlay gamePlay){
		log.debug("saving GamePlay instance");
		try {
			getSession().save(gamePlay);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}
	
	/**
	 * 根据主键查询
	 */
	public GamePlay findById(Integer id){
		log.debug("getting GamePlay instance with id: " + id);
		try {
			Criteria cri=getSession().createCriteria(GamePlay.class);
			cri.add(Expression.eq("id",id));
			return (GamePlay)cri.uniqueResult();
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}
	
	/**
	 * 删除游戏玩法
	 */
	public void delete(GamePlay gamePlay) {
		log.debug("deleting GamePlay instance");
		try {
			GamePlay gp=this.findById(gamePlay.getId());
			getSession().delete(gp);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}
	

	
	/**
	 * 根据属性查询
	 */
	public List findByProperty(String propertyName, Object value) {
		log.debug("finding GamePlay instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from GamePlay as model where model."
					+ propertyName + "= ?";
			Query queryObject = getSession().createQuery(queryString);
			queryObject.setParameter(0, value);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}
	
	/**
	 * 根据游戏编号关联查询，返回游戏玩法的集合
	 */
	public List findByGameNo(Integer gameno){
		log.debug("finding GamePlay instances by GameNo");
		try{
			Criteria cri=getSession().createCriteria(GamePlay.class);
			cri.createAlias("game", "g");
			cri.add(Expression.eq("g.gameno", gameno));
			System.out.println("查询出游戏玩法"+cri.list().size());
			return cri.list();
		}catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}
	
	/**
	 * 根据玩法名称查询
	 */
	public List findByname(Object name) {
		return findByProperty("name",name);
	}
	
	/**根据脚本类型查询
	 * 
	 */
	public List findByScriptType(Object scriptType){
		return findByProperty("scriptType",scriptType);
	}
	
	/**
	 * 查询所有游戏玩法
	 */
	public List findAll(){
		String queryString = "from GamePlay";
		Query queryObject = getSession().createQuery(queryString);
		return queryObject.list();
	}
	
	/** 
	 * 更行游戏玩法方法
	 */
	public void merge(GamePlay detachedInstance) {
		log.debug("merging GamePlay instance");
		try {
			getSession().saveOrUpdate(detachedInstance);
			log.debug("merge successful");
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}
	
	/*
	 * (non-Javadoc)
	 * 
	 * @see com.dao.backstage.IGameDAO#attachDirty(com.newbingo.entity.Game)
	 */
	public void attachDirty(GamePlay instance) {
		log.debug("attaching dirty Game instance");
		try {
			getSession().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.dao.backstage.IGameDAO#attachClean(com.newbingo.entity.Game)
	 */
	public void attachClean(GamePlay instance) {
		log.debug("attaching clean Game instance");
		try {
			getSession().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}
	
	/** 
	 * 分页查询,根据游戏编号进行分页
	 */
	public Pager findByPage(int start, int limit,Integer gameno){
		Pager page=new Pager();
		Criteria cri=this.getSession().createCriteria(GamePlay.class);
		cri.createAlias("game", "game");
		cri.add(Expression.eq("game.gameno", gameno));
		cri.setProjection(Projections.rowCount());
		page.setTotalSize(((Number)cri.uniqueResult()).intValue());
		cri.setProjection(null);
		cri.setResultTransformer(Criteria.ROOT_ENTITY);
		cri.setFirstResult(start);
		cri.setMaxResults(limit);
		page.setData(cri.list());
		return page;
	}
	
}
